//
//  HLLoginViewController.m
//  zhuanxian
//
//  Created by zhaoliang chen on 2017/11/16.
//  Copyright © 2017年 huiling. All rights reserved.
//

#import "HLLoginViewController.h"
#import "LeftViewTextField.h"
#import "HLMainTabViewController.h"
#import "AppDelegate.h"

@interface HLLoginViewController ()
{
    BOOL isRemember;
}

@property(nonatomic,strong)LeftViewTextField* textFieldUserName;
@property(nonatomic,strong)LeftViewTextField* textFieldPassword;
@property(nonatomic,strong)UIButton* btnRemember;
@property(nonatomic,strong)UIButton* btnLogin;
@property(nonatomic,strong)UIButton* btnYouke;

@end

@implementation HLLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIScrollView* scrollView = [UIScrollView new];
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    UIView* contentView = [UIView new];
    [scrollView addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(scrollView);
        make.width.mas_equalTo(scrollView);
    }];
    
    UIImageView* imgLogo = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"hl_login_logo"]];
    [contentView addSubview:imgLogo];
    [imgLogo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(130);
        make.centerX.mas_equalTo(self.view);
    }];
    
    [contentView addSubview:self.textFieldUserName];
    [self.textFieldUserName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(imgLogo.mas_bottom).offset(50);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(self.view.mas_width).offset(-40);
    }];
    
    [contentView addSubview:self.textFieldPassword];
    [self.textFieldPassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.textFieldUserName.mas_bottom).offset(15);
        make.size.mas_equalTo(self.textFieldUserName);
    }];
    
    [contentView addSubview:self.btnRemember];
    [self.btnRemember mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.textFieldUserName);
        make.top.mas_equalTo(self.textFieldPassword.mas_bottom).offset(15);
    }];
    
    [contentView addSubview:self.btnLogin];
    [self.btnLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.btnRemember.mas_bottom).offset(15);
        make.size.mas_equalTo(self.textFieldUserName);
    }];
    
    [contentView addSubview:self.btnYouke];
    [self.btnYouke mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.btnLogin.mas_bottom).offset(15);
        make.size.mas_equalTo(self.textFieldUserName);
    }];
    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.btnYouke.mas_bottom).offset(20);
    }];
    
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"username"] && [[NSUserDefaults standardUserDefaults] objectForKey:@"password"]) {
        self.textFieldUserName.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
        self.textFieldPassword.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
        isRemember = YES;
        [_btnRemember setImage:[UIImage imageNamed:@"hl_login_remember"] forState:UIControlStateNormal];
    } else {
        isRemember = NO;
        [_btnRemember setImage:[UIImage imageNamed:@"hl_login_remember_not"] forState:UIControlStateNormal];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:NO];
}

- (void)onClickRemember {
    isRemember = !isRemember;
    [self.btnRemember setImage:[UIImage imageNamed:isRemember?@"hl_login_remember":@"hl_login_remember_not"] forState:UIControlStateNormal];
    if (!isRemember) {
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"username"];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"password"];
        [[NSUserDefaults standardUserDefaults]synchronize];
    } else {
        if (self.textFieldUserName.text.length > 0) {
            [[NSUserDefaults standardUserDefaults]setObject:self.textFieldUserName.text forKey:@"username"];
            [[NSUserDefaults standardUserDefaults]setObject:self.textFieldPassword.text forKey:@"password"];
            [[NSUserDefaults standardUserDefaults]synchronize];
        }
    }
}

- (void)onClickLogin:(UIButton*)sender {
    [self.view endEditing:YES];
    if (sender.tag == 1000) {
        if (self.textFieldUserName.text.length < 1) {
            [SVProgressHUD showErrorWithStatus:@"请输入账号!"];
            return;
        }
        if (self.textFieldPassword.text.length < 1) {
            [SVProgressHUD showErrorWithStatus:@"请输入密码!"];
            return;
        }
    }
    [SVProgressHUD showWithStatus:@"登录中..."];
    [HLWebManager loginIn:sender.tag == 1000?self.textFieldUserName.text:@"chengke" password:sender.tag == 1000?self.textFieldPassword.text:@"z111111" successBlock:^(id response) {
        [SVProgressHUD dismiss];
        if ([response[@"code"]integerValue]==1) {
            HLUserObject* user = [HLUserObject mj_objectWithKeyValues:response];
            if (isRemember) {
                user.username = self.textFieldUserName.text;
                user.password = self.textFieldPassword.text;
                [[NSUserDefaults standardUserDefaults] setObject:user.username forKey:@"username"];
                [[NSUserDefaults standardUserDefaults] setObject:user.password forKey:@"password"];
            }
            [[NSUserDefaults standardUserDefaults]synchronize];
            AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
            HLMainTabViewController* vc = [[HLMainTabViewController alloc]init];
            appDelegate.window.rootViewController = vc;
            [self dismissViewControllerAnimated:NO completion:nil];
        } else {
            [SVProgressHUD showErrorWithStatus:response[@"message"]];
        }
    } failureBlock:^(NSError *error) {
        [SVProgressHUD dismiss];
    }];
}

- (LeftViewTextField*)textFieldUserName {
    if (!_textFieldUserName) {
        _textFieldUserName = [[LeftViewTextField alloc]init];
        UIImageView *userNameLeftView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"hl_login_user"]];
        userNameLeftView.contentMode= UIViewContentModeCenter;
        userNameLeftView.frame= CGRectMake(10,0,20,20);
        _textFieldUserName.leftViewMode= UITextFieldViewModeAlways;
        _textFieldUserName.leftView= userNameLeftView;
        _textFieldUserName.placeholder = @"请输入账号";
        [_textFieldUserName setAutocorrectionType:UITextAutocorrectionTypeNo];
        [_textFieldUserName setAutocapitalizationType:UITextAutocapitalizationTypeNone];
        _textFieldUserName.layer.cornerRadius = 20;
        _textFieldUserName.layer.borderColor = MainColor.CGColor;
        _textFieldUserName.layer.borderWidth = 0.5;
    }
    return _textFieldUserName;
}

- (LeftViewTextField*)textFieldPassword {
    if (!_textFieldPassword) {
        _textFieldPassword = [[LeftViewTextField alloc]init];
        UIImageView *userNameLeftView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"hl_login_password"]];
        userNameLeftView.contentMode= UIViewContentModeCenter;
        userNameLeftView.frame= CGRectMake(10,0,20,20);
        _textFieldPassword.secureTextEntry = YES;
        //左视图默认是不显示的 设置为始终显示
        _textFieldPassword.leftViewMode= UITextFieldViewModeAlways;
        _textFieldPassword.leftView= userNameLeftView;
        _textFieldPassword.placeholder = @"请输入密码";
        [_textFieldPassword setAutocorrectionType:UITextAutocorrectionTypeNo];
        [_textFieldPassword setAutocapitalizationType:UITextAutocapitalizationTypeNone];
        _textFieldPassword.layer.cornerRadius = self.textFieldUserName.layer.cornerRadius;
        _textFieldPassword.layer.borderColor = self.textFieldUserName.layer.borderColor;
        _textFieldPassword.layer.borderWidth = self.textFieldUserName.layer.borderWidth;
    }
    return _textFieldPassword;
}

- (UIButton*)btnRemember {
    if (!_btnRemember) {
        _btnRemember = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnRemember setImage:[UIImage imageNamed:@"hl_login_remember_not"] forState:UIControlStateNormal];
        [_btnRemember setTitle:@"记住密码" forState:UIControlStateNormal];
        [_btnRemember setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _btnRemember.titleLabel.font = [UIFont systemFontOfSize:13];
        [_btnRemember addTarget:self action:@selector(onClickRemember) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnRemember;
}

- (UIButton*)btnLogin {
    if (!_btnLogin) {
        _btnLogin = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnLogin.backgroundColor = MainColor;
        _btnLogin.tag = 1000;
        [_btnLogin setTitle:@"登录" forState:UIControlStateNormal];
        [_btnLogin setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btnLogin addTarget:self action:@selector(onClickLogin:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnLogin;
}

- (UIButton*)btnYouke {
    if (!_btnYouke) {
        _btnYouke = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnYouke.backgroundColor = MainColor;
        _btnYouke.tag = 1001;
        [_btnYouke setTitle:@"游客登录" forState:UIControlStateNormal];
        [_btnYouke setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btnYouke addTarget:self action:@selector(onClickLogin:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnYouke;
}


@end
